import { z } from "zod"

export const SessionSchema = {
  id: "",
  conversationID: "",
  toAccount: "",
  type: "",
  subType: "",
  lastMessage: {
    lastTime: 1756431122,
    lastSequence: 1353113262,
    fromAccount: "@RBT#001",
    type: "TIMTextElem",
    payload: {
      text: "",
    },
    cloudCustomData: "",
    isRevoked: false,
    onlineOnlyFlag: false,
    nick: "",
    nameCard: "",
    version: 0,
    isPeerRead: false,
    revoker: "",
    messageForShow: "",
  },
  unreadCount: 0,
  peerReadTime: 0,
  groupAtInfoList: [],
  groupProfile: {
    groupID: "",
    name: "",
    avatar: "",
    type: "Public",
    introduction: "",
    notification: "",
    ownerID: "",
    createTime: 1727142895,
    infoSequence: 1056,
    lastInfoTime: 1756713087,
    selfInfo: {
      messageRemindType: "AcceptAndNotify",
      joinTime: 1742461790,
      nameCard: "",
      role: "Member",
      userID: "admin",
      readedSequence: 340,
      excludedUnreadSequenceList: [],
    },
    lastMessage: {
      lastTime: 1756712436,
      lastSequence: 340,
      fromAccount: "zhangal",
      type: "TIMTextElem",
      payload: {
        text: "",
      },
      cloudCustomData: "",
      isRevoked: false,
      onlineOnlyFlag: false,
      nick: "",
      nameCard: "",
      revoker: null,
      messageForShow: "",
    },
    nextMessageSeq: 341,
    memberCount: 9,
    maxMemberCount: 6000,
    joinOption: "NeedPermission",
    inviteOption: "DisableInvite",
    groupCustomField: [],
    muteAllMembers: false,
    isSupportTopic: false,
    muteAllMember: "Off",
  },
  userProfile: {
    userID: "",
    nick: "",
    gender: "",
    birthday: 0,
    location: "",
    selfSignature: "",
    allowType: "",
    language: 0,
    avatar: "",
    messageSettings: 0,
    adminForbidType: "",
    level: 0,
    role: 0,
    lastUpdatedTime: 0,
    profileCustomField: [],
  },
  remark: "",
  isPinned: false,
  pinned: 0,
  messageRemindType: "",
  markList: [],
  customData: "",
  conversationGroupList: [],
  draftText: "",
  createdAt: 0,
  updatedAt: 0,
}

export const userProfileSchema = z
  .object({
    userID: z.string().optional(),
    nick: z.string().optional(),
    gender: z.string().optional(),
    birthday: z.number().optional(),
    location: z.string().optional(),
    selfSignature: z.string().optional(),
    allowType: z.string().optional(),
    language: z.number().optional(),
    avatar: z.string().optional(),
    messageSettings: z.number().optional(),
    adminForbidType: z.string().optional(),
    level: z.number().optional(),
    role: z.number().optional(),
    lastUpdatedTime: z.number().optional(),
    profileCustomField: z.array(z.any()).optional(),
  })
  .optional()

export const SessionTypeSchema = z.enum(["C2C", "GROUP", "@TIM#SYSTEM"])

export const DB_SessionSchema = z.object({
  id: z.string().optional(),
  conversationID: z.string(),
  toAccount: z.string().optional(),
  type: SessionTypeSchema,
  subType: z.string().optional(),
  lastMessage: z
    .object({
      lastTime: z.number().optional(),
      lastSequence: z.number().optional(),
      fromAccount: z.string().optional(),
      type: z.string().optional(),
      payload: z
        .object({
          text: z.string().optional(),
        })
        .optional(),
      cloudCustomData: z.string().optional(),
      isRevoked: z.boolean().optional(),
      onlineOnlyFlag: z.boolean().optional(),
      nick: z.string().optional(),
      nameCard: z.string().optional(),
      version: z.number().optional(),
      isPeerRead: z.boolean().optional(),
      revoker: z.string().optional(),
      messageForShow: z.string().optional(),
    })
    .optional(),
  unreadCount: z.number().int().min(0).optional(),
  peerReadTime: z.number().optional(),
  groupAtInfoList: z.array(z.any()).optional(),
  groupProfile: z.any().optional(),
  userProfile: userProfileSchema,
  remark: z.string().optional(),
  isPinned: z.boolean().optional(),
  pinned: z.number().int().min(0).max(1).optional(),
  messageRemindType: z.string().optional(),
  markList: z.array(z.any()).optional(),
  customData: z.string().optional(),
  conversationGroupList: z.array(z.any()).optional(),
  draftText: z.string().optional(),
  createdAt: z.number().optional(),
  updatedAt: z.number().optional(),
})

export type DB_Session = z.infer<typeof DB_SessionSchema>
